﻿using System;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AMS.Inventory.PDA.Helper;

namespace AMS.Inventory.PDA.UserControls
{
    public partial class XmlEnumCtl : UserControl
    {
        /// <summary>
        /// 获取或设置下拉框的宽度(默认152px)
        /// </summary>
        public new int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                cboList.Width = value;
            }
        }



        /// <summary>
        /// 获取或设置所选项的索引
        /// </summary>
        public int SelectedIndex
        {
            get { return cboList.SelectedIndex; }
            set { cboList.SelectedIndex = value; }
        }

        /// <summary>
        /// 获取或设置所选项的值
        /// </summary>
        public string SelectedValue
        {
            get
            {
                if (cboList.SelectedValue == null)
                    return "";
                return cboList.SelectedValue.ToString();
            }
            set { cboList.SelectedValue = value; }
        }

        /// <summary>
        /// 获取列表控件中索引最小的选定项
        /// </summary>
        public object SelectedItem
        {
            get { return cboList.SelectedItem; }
        }


        /// <summary>
        /// 
        /// </summary>
        public new bool Enabled
        {
            get { return cboList.Enabled; }
            set { cboList.Enabled = value; }
        }

        public override string Text
        {
            get
            {
              return cboList.SelectedValue.ToString();
                //return cboList.Text.ToString();
            }
            set
            {
                SetVal (value);
            }
        }

        public override Color BackColor
        {
            get { return cboList.BackColor; }
            set { cboList.BackColor = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        public string EnumNodeName { get; set; }

        public XmlEnumCtl()
        {
            InitializeComponent();
            
        }

        public void SetData()
        {
            var enumHlp = new XmlEnumHelper();
            //cboList.Items.Clear();
            DataTable dt = enumHlp.GetDataTableVal(EnumNodeName);
            if(dt!=null&&dt.Rows.Count>0&& dt.Columns.Count>1)
            {
                cboList.DataSource = dt;
                cboList.DisplayMember = dt.Columns[1].ColumnName;
                cboList.ValueMember = dt.Columns[0].ColumnName;
            }
            
        }

        public void SetVal(string val)
        {
            SetData();
            if (!string.IsNullOrEmpty(val))
                cboList.SelectedValue = val;
        }

    }
}
